package com.handersome.util;

import java.net.URL;
import java.util.List;

import com.handersome.type.EhCacheType;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 
 * @author handersome
 *
 * 创建时间:2017年12月12日上午11:03:53
 */
public class EhcacheUtil {

	private static final String PATH = "/ehcache.xml";
	
	/**
	 * 用户cache块
	 */
	private static final String USER_CACHE = "user_cache";

	private URL url;

	private CacheManager manager;

	private static EhcacheUtil ehCache;

	private EhcacheUtil(String path) {
		url = getClass().getResource(path);
		manager = CacheManager.create(url);
	}

	public static EhcacheUtil getInstance() {
		if (ehCache == null) {
			ehCache = new EhcacheUtil(PATH);
		}
		return ehCache;
	}

	/**
	 * create cache(不会过期)
	 * 
	 * @param name
	 * @return
	 */
	public Cache createCache(String name) {
		Cache cache = new Cache(name, 10000, false, true, Integer.MAX_VALUE, Integer.MAX_VALUE);
		manager.addCache(cache);
		return (Cache) manager.getCache(name);
	}

	/**
	 * 返回cache的size
	 * 
	 * @param cache
	 * @return
	 */
	public int getcacheSize(String cacheName) {
		Cache cache = (Cache) manager.getCache(cacheName);
		return cache.getSize();
	}
	
	/**
	 * 判断cache块中是否存在key
	 * 
	 * @param keyName
	 * @param cacheName
	 * @return
	 */
	public boolean isExist(String keyName, String cacheName) {
		Cache cache = (Cache) manager.getCache(cacheName);
		return cache.isKeyInCache(keyName);
	}

	/**
	 * 在已知cache中添加键值对（element级别上设置过期时间）
	 * 
	 * @param cacheName
	 *            cache名称
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param time 过期时间(秒)
	 * 
	 * @param eternal 是否永不过期
	 *            
	 */
	public void put(String cacheName, String key, Object value, boolean eternal, int timeToLiveSeconds) {
		Cache cache = (Cache) manager.getCache(cacheName);
		Element element = new Element(key, value);
		element.setEternal(eternal);
		element.setTimeToLive(timeToLiveSeconds);
		cache.put(element);
	}
	 
	public void putForever(String cacheName, String key, Object value){
		put(cacheName, key, value, true, 100);
	}
	
	public void put(String cacheName, String key, Object value, int timeToLiveSeconds){
		put(cacheName, key, value, false, timeToLiveSeconds);
	}

	/**
	 * 获得已知cache的键值对
	 * 
	 * @param cacheName
	 *            cache名称
	 * @param key
	 *            键
	 * @return
	 */
	public Object get(String cacheName, String key) {
		Cache cache = (Cache) manager.getCache(cacheName);
		Element element = cache.get(key);
		return element == null ? null : element.getObjectValue();
	}
	
	/**
	 * 从用户cache块获取key
	 * @param key
	 * @return
	 */
	public Object getFromUserCache(String key){
		Cache cache = (Cache) manager.getCache(USER_CACHE);
		Element element = cache.get(key);
		return element == null ? null : element.getObjectValue();
	}

	/**
	 * 获得已知cache
	 * 
	 * @param cacheName
	 * @return
	 */
	public Cache get(String cacheName) {
		return (Cache) manager.getCache(cacheName);
	}

	/**
	 * 移除已知cache的键值对
	 * 
	 * @param cacheName
	 * @param key
	 */
	public void remove(String cacheName, String key) {
		Cache cache = (Cache) manager.getCache(cacheName);
		cache.remove(key);
	}
	
	/**
	 * 从用户cache块移除key
	 * @param key
	 */
	public void removeFromUserCache(String key){
		remove(USER_CACHE, key);
	}
	
	public static void main(String[] args) {
		String key = "key";
		String value = "hello";
		EhcacheUtil.getInstance().createCache("mytest");
		EhcacheUtil.getInstance().put("mytest", key, value, false, 1000);;
		System.out.println(EhcacheUtil.getInstance().get("mytest", key));
	}

}
